package com.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;

public class TempMax extends Configured implements Tool {
    // 新建map类
    static class TempMaxMapper extends
            Mapper<LongWritable, Text, Text, IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
//            key:k1(偏移量)		value:v1（数据）
//          0				2020,26
//          ...				2017,34
//          ...				2016,22
//				            2017,30
//				            2019,29

            String[] datas = value.toString().split(",");
            // datas:["2020","26"]
            if (datas.length==2){
                String k2 = datas[0];
                int v2 = Integer.parseInt(datas[1]);
                context.write(new Text(k2),new IntWritable(v2));
            }
            //          ->map(求每年的最高温度)
//          k2					v2
//          2020					26
//          2017					34
//          2016					22
//          2017					30
//          2019					29
        }
    }

    static class TempMaxReducer extends
            Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
//            key:k2			  values:v2
//            2020					[26]
//            2017					[34,30]
//            2016					[22]
//            2019					[29]
            int max = 0;
            for (IntWritable val:values){
                // temp=34
                // temp=30
                int temp = val.get();
                if (max < temp){
                    // max = 34
                    max = temp;
                }
            }
            context.write(key,new IntWritable(max));
//->reduce
//            k3					v3
//            2020					26
//            2017					34
//            2016					22
//            2019					29

        }
    }

    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        // 指定输入输出路径
        Path input = new Path(
                "hdfs://192.168.10.11:9000/weadata");
        Path output = new Path(
                "hdfs://192.168.10.11:9000/weaoutput");

        FileSystem fs = FileSystem.get(
                new URI("hdfs://192.168.10.11:9000")
                ,conf);
        if (fs.exists(output)) fs.delete(output,true);
        //构建Job
        Job job = Job.getInstance(conf);
        job.setJobName("temp");
        job.setJarByClass(this.getClass());
        // 指定map类的配置
        job.setMapperClass(TempMaxMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        // 设置reduce个数，也就是分区数
        job.setNumReduceTasks(2);
        // 指定reduce类的配置
        job.setReducerClass(TempMaxReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        // 指定输入处理器
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job,input);
        // 指定输出处理器
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,output);

        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new TempMax(),args));
    }
}
